package tech.aistar.controller.foster;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.foster.Event;
import tech.aistar.entity.foster.Foster;
import tech.aistar.entity.foster.Room;
import tech.aistar.entity.mycat.Cat;
import tech.aistar.entity.personal.User;
import tech.aistar.service.foster.IEventService;
import tech.aistar.service.foster.IFosterService;
import tech.aistar.service.foster.IRoomService;
import tech.aistar.service.mycat.ICatService;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/foster")
public class FosterController {

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IFosterService fosterService;

    @Autowired
    private ICatService catService;

    @Autowired
    private IEventService eventService;

    @RequestMapping("/myCatView")
    public String myCat(Model model,Integer catId){

        Cat cat = catService.findById(catId);

        System.out.println(cat);

        model.addAttribute("cat",cat);

        return "cat/myCat";
    }


    @RequestMapping("/fosterCreate")
    public String fosterCreateAction(Foster foster, HttpServletRequest request){
        //待添加
        //添加order
        //回显order的id并赋值给foster的orderId

        foster.setOrderId(1);
        //用户id通过session中获取
        User user = (User) request.getSession().getAttribute("user");
        //给foster设置id
        //foster.setUserId(user.getId());
        foster.setUserId(1);
        //status 是判断是否删除 0 为存在 1 为删除
        foster.setStatus(0);

        System.out.println(foster);

        fosterService.insert(foster);

        //添加成功后需将对应房间型号的房间的数量建议
        roomService.fosterAdd(foster.getRoomId());

        return "redirect:fosterView";
    }


    //跳转到寄养创建界面
    @RequestMapping("/fosterCreateView")
    public String fosterCreateView(Model model,HttpServletRequest request){

        //通过session获取用户信息
//        User user = (User) request.getSession().getAttribute("user");
        //通过catService获取该用户的猫咪并发送给页面
//        List<Cat> cats = catService.findByUserId(user.getId());
//        model.addAttribute("cats",cats);

        //获取房间信息
        List<Room> rooms = roomService.findAll();
        model.addAttribute("rooms",rooms);


        //获取服务列表
        List<Event> events = eventService.findAll();
        model.addAttribute("events",events);

        return "foster/fosterCreate";
    }

    //跳转到寄养介绍界面
    @RequestMapping("/fosterView")
    public String fosterView(){
        return "foster/foster";
    }



    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
